package com.sangeng.service.serviceimpl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.dto.AddLinkDto;
import com.sangeng.domain.dto.UpdateCategoryDto;
import com.sangeng.domain.entity.Category;
import com.sangeng.domain.entity.Link;
import com.sangeng.domain.vo.LinkVo;
import com.sangeng.domain.vo.PageVo;
import com.sangeng.enums.AppHttpCodeEnum;
import com.sangeng.exception.SystemException;
import com.sangeng.mapper.LinkMapper;
import com.sangeng.service.LinkService;
import com.sangeng.utils.BeanCopyUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {
    @Override
    public ResponseResult<PageVo> getLinkList(Integer pageNum, Integer pageSize, String name, String status) {
        Page<Link> linkPage = new Page<>(pageNum, pageSize);
        //查询未被删除的link 由于设置了自动拼接del_flg查询，所以传入wrapper不用自己填
        Page<Link> page = page(linkPage, null);
        List<Link> linklist = page.getRecords();
        List<LinkVo> linkVoList = BeanCopyUtils.copyBeanList(linklist, LinkVo.class);
        long total = page.getTotal();
        PageVo pageVo = new PageVo(linkVoList, total);
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult<String> addLink(AddLinkDto linkDto) {
        //数据体对应的实体类是Link 要先进行转换然后才能存入
        Link link = BeanCopyUtils.copyBean(linkDto, Link.class);
        boolean save = save(link);
        if (save) {
            return ResponseResult.okResult();
        } else {
            throw new SystemException(AppHttpCodeEnum.LINK_INSERT_FAILED);
        }
    }

    //修改友链
    private final LinkMapper linkMapper;
    @Override
    public ResponseResult<LinkVo> getLinkById(Long id) {
        Link link = linkMapper.selectById(id);
        LinkVo linkVo = BeanCopyUtils.copyBean(link, LinkVo.class);
        return ResponseResult.okResult(linkVo);
    }

    @Override
    public ResponseResult<String> updateLink(LinkVo linkVo) {
        Link link = BeanCopyUtils.copyBean(linkVo, Link.class);
        boolean update = updateById(link);
        if (update) {
            return ResponseResult.okResult();
        } else {
            throw new SystemException(AppHttpCodeEnum.LINK_UPDATE_ERROR);
        }
    }

    //删除
    @Override
    public ResponseResult<String> deleteLinkById(List<Long> idList) {
        if(CollectionUtils.isNotEmpty((idList))){
            boolean update = lambdaUpdate().set(Link::getDelFlag, 1).in(Link::getId, idList).update();
            return update ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.LINK_DELETE_ERROR);
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_ERROR);
    }

}
